<?php 
/**
 * 电信短信接口
 * @author liuyang
 *
 */  
namespace Common\Service\Sms;
use Common\ServiceInterface\SmsService;;
use Common\Util\HttpUtil;
class  Sms189ServiceImpl implements SmsService { 
    //private $app_id = '182999800000039655';// 应用ID
    private $app_id = '384042720000248048';
    //private $app_secret = '4157a21f94dd5f4d12d47e4f8242cf9e';// 应用的密钥
    private $app_secret = 'c5611ac5eacef704efa3f899fffb7fea';// 应用的密钥
    private $timestamp=''; //时间戳，格式为：yyyy-MM-dd hh:mm:ss 
    private $phone='';
    private $code; //验证码格式必须为6位的纯数字
    private $codelen = 6; //验证码长度必须为6位
    private $charset = "0123456789"; //验证码的随机因子
    private $exp_time=2; //验证码有效期长度，单位是分钟，可以为空，默认有效2分钟
    private $grant_type='client_credentials'; //Client Credentials授权模式
    private $access_token_url = 'https://oauth.api.189.cn/emp/oauth2/v2/access_token';// 获取access_token的URL
    private $send_url = 'http://api.189.cn/v2/dm/randcode/sendSms';// 自定义短信发送信息的URL
    private $token_url = 'http://api.189.cn/v2/dm/randcode/token';// 获取token的URL 
 
    /**
     * (non-PHPdoc)
     * @see SmsService::sendInfo()
     * return int
     */
    public function sendInfo($phone,$content){ 
        $this->timestamp = date('Y-m-d H:i:s');
        $this->phone=$phone;
        $this->createCode();
        $rand=0; 
        if($this->checkPhone($this->phone)){
            if (!$this->send($content)->res_code){ //从发送短信的方法中返回的obj中读取发送成功与否的标识
                $rand=$this->getCode(); 
            } 
        } 
        return $rand;
    }
     
    
    //检查参数
    private function checkPhone($phone){
        if (preg_match('/^(1[0-9]{10})$/',$phone)){
            $this->phone=$phone;
            return true;
        }else{
            return false;
        }
    }
    //生成随机码
    private function createCode() {
        $_len = strlen($this->charset)-1;
        for ($i=0;$i<$this->codelen;$i++) {
            $this->code .= $this->charset[mt_rand(0,$_len)];
        }
    }
    //获取access_token
    private function getAccessKey() {
        $param['grant_type']= "grant_type=".$this->grant_type;
        $param['app_id'] = "app_id=".$this->app_id;
        $param['app_secret'] = "app_secret=".$this->app_secret;
        $plaintext = implode("&",$param);
        $result = HttpUtil::curlPost($this->access_token_url,$plaintext);
        $obj = json_decode($result);
        return $obj->access_token;
    }
    //获取Token
    private function getToken($access_token) {
        $param['app_id']= "app_id=".$this->app_id;
        $param['access_token'] = "access_token=".$access_token;
        $param['timestamp'] = "timestamp=".$this->timestamp;
        ksort($param);
        $plaintext = implode("&",$param);
        $param['sign'] = "sign=".rawurlencode(base64_encode(hash_hmac("sha1", $plaintext, $this->app_secret, $raw_output=True)));
        ksort($param);
        $param['timestamp'] = "timestamp=".urlencode($this->timestamp);
        $this->token_url .= '?'.implode("&",$param);
        $result = HttpUtil::curlGet($this->token_url);
        $obj = json_decode($result);
        return $obj->token;
    }
    //获取验证码
    private function getCode() {
        return $this->code;
    }
    private function send($content) {
        $access_token=$this->getAccessKey();
        $token=$this->getToken($access_token);
        $param['app_id']= "app_id=".$this->app_id;
        $param['access_token'] = "access_token=".$access_token;
        $param['timestamp'] = "timestamp=".$this->timestamp;
        $param['token'] = "token=".$token;
        $param['phone'] = "phone=".$this->phone;
        $param['randcode'] = "randcode=".$this->code;
        //$param['url']="http://machineapi.weeguu.com/";
        if(!empty($this->exp_time)) $param['exp_time'] = "exp_time=".$this->exp_time;
        ksort($param);
        $plaintext = implode("&",$param);
        $param['sign'] = "sign=".rawurlencode(base64_encode(hash_hmac("sha1", $plaintext, $this->app_secret, $raw_output=True)));
        ksort($param);
        $str = implode("&",$param);
        $result = HttpUtil::curlPost($this->send_url,$str);
        $obj = json_decode($result); 
        return $obj;
    }
  
    
}